package divinity.fx.display
{
	import flash.display.MovieClip;
	
	import divinity.basic.*;
	import divinity.events.WorldEvent;
	import divinity.fx.particles.ParticleGenerator;
	import divinity.fx.vfx.AnimatedFX;
	import divinity.utils.Filters;

	public class DisplayFX
	{
		private var _shake:int=0;
		private var shakers:Array;
		private var _sx:Number;
		private var _sy:Number;		
		private var _shaking:Boolean=false;
		private var _world:DivinityWorld;
		private var _pg;
		/***
		 * 
		 * 
		 * 
		 * 
		 * */
		public function DisplayFX(wld:*)
		{
			_world=wld;
			super();
			_sx=_world.DISPLAY.x;
			_sy=_world.DISPLAY.y;
			Filters.addFilters(_world.OBJECTS,"DropShadowFilter,quality:3,distance:5");
			//Filters.addFilters(_world.BACKGROUND,"BlurFilter,quality:3,blurX:5,blurY:5");
			//Filters.addFilters(_world.VFX,"BlurFilter,quality:3,blurX:5,blurY:5");
			Filters.addFilters(_world.AIR,"DropShadowFilter,quality:3,distance:80,strength:.7");
			//_world.AIR.scaleX=1.5,
			//_world.AIR.scaleY=1.5;
			//_world.AIR.x=_world.BACKGROUND.x*-1.5;
			//_world.AIR.y=_world.BACKGROUND.x*-1.5;
			trace(this,_sx,_sy);
			
			
			_pg=new ParticleGenerator(_world);			
			_pg.def._attachedTo=_world.VFX;
			
		}
		/**
		 * 
		 * 
		 * 
		 * 
		 * */
		public function shake(numberOfShakes:int,shakeStength:int=5):void
		{	
			trace("making shakes "+_world.DISPLAY);
			_world.DISPLAY.x=_sx;
			_world.DISPLAY.y=_sy;
			_shake=0;		
			shakers=[];			
			for(var n=0;n<numberOfShakes;n++)
			{
				shakers[n]={};
				var normal:int=(1-(n/numberOfShakes))*shakeStength;				
				shakers[n].x=_sx+Math.random()*(normal*2)-normal;
				shakers[n].y=_sy+Math.random()*(normal*2)-normal;
			}				
			shakers.push({x:_sx,y:_sy})			
			makeShake();
			_world.addEventListener(WorldEvent.CYCLE,makeShake);
		}
		private function makeShake(e:WorldEvent=null):void
		{
			
			_world.DISPLAY.x=shakers[_shake].x;
			_world.DISPLAY.y=shakers[_shake].y;
			_shake++;
			
			if(_shake>shakers.length-1)
			{
				trace("SHAKE STOP");
				_world.removeEventListener(WorldEvent.CYCLE,makeShake);
				
			}
			else
			{
				trace("shaking");
			}
			
		}
		/**
		 * 
		 * 
		 * 
		 * 
		 * 
		 * */
		public function makeVFX(me:*,num:int,props:Object=null,reset:Boolean=false):void
		{		
			_pg.x=me.x;
			_pg.y=me.y;
			
			if(props!=null)
			{
				
				if(reset)
				{
					_pg.setProps(props);	
				}
				else
				{
					_pg.changeProps(props);	
				}
			}			
			_pg.oneTimeGen(num);			
		}
		/**
		 * 
		 * 
		 * 
		 * 
		 * */
		public function anim(animationName:String,point:Object,timeframe:int,scale:Number=1)
		{
			//	TODO: THIS WILL BE MODIFIED TO BE INTERACIVE
			var animAsset:MovieClip=new expl();
			var animFX:AnimatedFX=new AnimatedFX(_world,point.x,point.y,animAsset);	
			animFX.scaleX=scale;
			animFX.scaleY=scale;
			_world.VFX.addChild(animFX);
			animFX.play(animationName,timeframe);
		}
	}
	
}